﻿using Airthmetic;
using BaseTool;
using System;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;

namespace StructuredLightSystemUI
{
    /// <summary>
    /// 全局静态参数类
    /// </summary>
    internal static class MyGlobal
    {
        /// <summary> 版本 </summary>
        public static readonly string Versions = $"版本：{Assembly.GetExecutingAssembly().GetName().Version.ToString()}";

        /// <summary> 全局相机工具 </summary>
        public static CameraTool CameraTool = new CameraTool();
        /// <summary> 全局投影仪工具 </summary>
        public static ProjectorTool ProjectorTool = new ProjectorTool();
        /// <summary> 全局标定工具 </summary>
        public static CalibrationTool CalibrationTool = new CalibrationTool();
        /// <summary> 全局重建工具 </summary>
        public static ReconstructTool ReconstructTool = new ReconstructTool();

        /// <summary> 生成棋盘格工具 </summary>
        public static CheckerboardImageTool CheckerboardImageTool = new CheckerboardImageTool();
        /// <summary> 生成条纹光栅图像工具 </summary>
        public static StripedGratingImageTool StripedGratingImageTool = new StripedGratingImageTool();


        #region 初始化
        /// <summary>
        /// 初始化(仅一次)
        /// </summary>
        public static void Init()
        {
            try
            {
                //读取当前产品型号
                PathTool.ReadProduct();

                //加载配置
                Load();
                Machine.ChangeProductAction?.Invoke();

                Log.SaveLog($"初始化成功", LogType.Run, Color.Green);
            }
            catch (Exception ex)
            {
                Log.SaveError(ex, $"初始化异常，{ex.Message}", LogType.Run);
            }
        }
        /// <summary>
        /// 加载
        /// </summary>
        private static void Load()
        {
            try
            {
                //相机工具停止视频流
                if (CameraTool.IsGrabbing)
                    CameraTool.StopGrab();
                //相机工具参数加载
                CameraTool.Read();

                //投影仪串口停止
                if (ProjectorTool.IsOpen)
                    ProjectorTool.Close();
                //投影仪工具参数加载
                ProjectorTool.Read();

                //标定工具参数加载
                CalibrationTool.Read();
                //重建工具参数加载
                ReconstructTool.Read();

                //读取生成棋盘格工具参数
                CheckerboardImageTool.Read();
                //读取生成条纹光栅图像工具参数
                StripedGratingImageTool.Read();

                Log.SaveLog($"加载全部参数完成", LogType.Run, Color.Green);
            }
            catch (Exception ex)
            {
                Log.SaveError(ex, $"加载全部参数异常，{ex.Message}", LogType.Run);
            }
        }

        /// <summary>
        /// 切换配置
        /// </summary>
        /// <param name="newName">新配置名字</param>
        public static void ChangeProduct(string newName)
        {
            string oldName = PathTool.ProductType;
            if (oldName == newName)
            {
                Log.SaveLog($"无需切换配置", LogType.Run, Color.Black);
                return;
            }
            try
            {
                PathTool.ProductType = newName;

                //加载配置
                Load();
                Machine.ChangeProductAction?.Invoke();

                //保存配置名
                PathTool.SaveProduct();

                Log.SaveLog($"切换配置（【{oldName}】 -> 【{newName}】）成功", LogType.Run, Color.Green);
            }
            catch (Exception ex)
            {
                PathTool.ProductType = oldName;
                Log.SaveError(ex, $"切换配置（【{oldName}】 -> 【{newName}】）异常，{ex.Message}");
            }
        }

        #endregion
    }
    /// <summary> 页面选项 </summary>
    internal enum ToolStripMenuPage { 无, 主页面, 相机测试页面, 投影仪测试页面, 标定页面, 重建页面, 点云显示页面, 辅助功能页面 }
}
